import * as cc from "cc";
import VCPageViewDemo from "../vc/VCPageViewDemo";
import { ViewBase } from './../../../../framework/view/ViewBase';
import PageView from "../../../../framework/components/scroll_view/list_view/PageView";
import { ListViewParams } from "../../../../framework/components/scroll_view/list_view/ListView";
import { Loger } from "../../../../framework/loger/Loger";

const { ccclass, property, menu } = cc._decorator;
@ccclass("ViewPageViewDemo")
@menu("GameView/ViewPageViewDemo")
export default class ViewPageViewDemo extends ViewBase{

    protected static prefabUrl:string = "game/prefab/mvc/list_view_demo/PageViewDemo";
    public static className:string = "ViewPageViewDemo"
    protected vc:VCPageViewDemo = null;

    
    /**列表滚动容器 */
    @property({ type: PageView, tooltip: "列表容器" })
    pageView: PageView = null;


    btnRefresh: cc.Node
    btnSetPos: cc.Node
    btnLeft: cc.Node
    btnRight: cc.Node
    //生命周期：onInit(加载时只调一次)==>onOpen(每次showui时)==>onShow()==>onStart()==>onHide()==>onClose()
    /**只会在首次加载时调用一次，可以用于获取节点，绑定事件 */
    protected onInit() {
        super.onInit();
        this.btnRefresh = this.node.getChildByName("btnRefresh")
        this.setClickListener(this.btnRefresh, this.onClickRefresh, this);
        this.btnSetPos = this.node.getChildByName("btnSetPos")
        this.setClickListener(this.btnSetPos, this.onClickSetPos, this);
        this.btnLeft = this.node.getChildByName("btnLeft")
        this.setClickListener(this.btnLeft, this.onClickLeft, this);
        this.btnRight = this.node.getChildByName("btnRight")
        this.setClickListener(this.btnRight, this.onClickRight, this);

    }
    /**每次打开UI前，onStart前,每次呼出前赋值传参的时候用 */
    protected onOpen(){
        super.onOpen();
        this.refreshView();
    }

    /**开始动画播完 onShow之后*/
    protected onStart(){
        super.onStart();
    }

    /**关闭动画结束后 */
    protected onClose(){
        super.onClose();
    }

    /**update生命周期 */
    protected onUpdate(dt) {
    }


    onClickRefresh() {
        let params: ListViewParams = {
            arr_data: this.vc.data2,
            vc: this.vc,
            is_clean: true,
            interval: 0.1
        }
        this.pageView.reloadData(params);
        Loger.logConfig("---点击刷新---")
    }

    onClickSetPos() {
        this.pageView.setPageIndex(50)
        Loger.logBusiness("---点击定位---", "onClickSetPos")
    }


    onClickLeft(){
        this.pageView.addPageIndex(-1);
    }
    
    onClickRight(){
        this.pageView.addPageIndex(1);
        
    }

    refreshView() {
        this.pageView && this.pageView.reloadData({
            arr_data: this.vc.data,
            vc: this.vc,
            from_index: 5,
            interval: 0.1,
            on_page_turn:(curIdx) =>{
                this.onPageTurn()
            },
        }).then(() => {
            Loger.log("pageView load done")
        });

    }

    onPageTurn(){
        let curIdx = this.pageView.getCurrentPageIndex();
        this.btnLeft.active = curIdx != 0;
        this.btnRight.active = curIdx != this.vc.data.length - 1;
    }
}